class code4 {
    //岛屿数量
    int count;
    int m;
    int n;
    public int numIslands(char[][] grid) {
        m = grid.length;
        n = grid[0].length;

        for(int i=0;i<m;i++) {
            for(int j=0;j<n;j++) {
                if(grid[i][j] == '1') {
                    count++;
                    dfs(grid,i,j);
                }
            }
        }
        return count;
    }

    public void dfs(char[][] grid,int x, int y) {
        //出口
        if(board(x,y)) {
            return;
        }

        if(grid[x][y] != '1') {
            return;
        }

        grid[x][y] = 2;

        dfs(grid,x-1,y);
        dfs(grid,x+1,y);
        dfs(grid,x,y-1);
        dfs(grid,x,y+1);
    }
    private boolean board(int x,int y) {
        if(x<0 || y<0 || x>=m || y>=n) {
            return true;
        }
        return false;
    }
}